<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function js_ajax(url) {
        return new Promise((resolve, reject) => {
          var xhr = new XMLHttpRequest();
          xhr.open("get", url, true);
          xhr.send();
          xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
              if (xhr.status == 200) {
                var data = JSON.parse(xhr.responseText);
                resolve(data);
              } else {
                reject("404");
              }
            }
          };
        });
      }

      js_ajax("a.json").then((res) => {
        console.log(res);
      });
      //一个异步请求时的写法 （封装原生的ajax）

      //  axios返回的也是一个promise对象

      js_ajax("a.json")
        .then((res) => {
          console.log(res);

          return js_ajax("b.json");
        })
        .then((res) => {
          console.log(res);
          return js_ajax("c.json");
        })
        .then((res) => {
          console.log(res);
        });
      // 用promise实现请求回调地狱，回调地狱指的是回调函数层层嵌套，用promise变成le链式写法，不再有很深的嵌套
    </script>
  </body>
</html>
